﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using CMS.Web.Data;

namespace CMS.Web.Engine
{
    public class WidgetBase : IWidget
    {
        public int ID { get; set; }
        public string Zone { get; set; }
        public int Order { get; set; }
        public string PartialViewName { get; set; }
        public IWidgetModel Model { get; set; }
        public IWidgetSettings Settings { get; set; }
        public WidgetAttribute Annotation
        {
            get
            {
                WidgetAttribute value = GetType().GetCustomAttributes(typeof(WidgetAttribute), true).OfType<WidgetAttribute>().FirstOrDefault();
                if (value.IsNull())
                    value = new WidgetAttribute();
                return value;
            }
        }

        public virtual void Initialize(string settings, IEnumerable<string> ids)
        { }        

        public virtual void ProcessPost()
        { }
    }

    public class WidgetModel : IWidgetModel
    {
        private List<Exception> _errors = new List<Exception>();
        public List<Exception> Errors
        {
            get { return _errors; }          
        }
        public int Widget { get; set; }
        public string Prefix 
        { 
            get { return string.Format("widget{0}", Widget); } 
        }        
    }

    public class WidgetCategoryModel
    {
        private List<WidgetDetails> _widgets = new List<WidgetDetails>();
        public class WidgetDetails
        {
            public int Index { get; set; }
            public bool Checked { get; set; }
            public Guid Guid { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            
            public WidgetDetails(string guid, string name, string description, int index)
            {
                this.Guid = new Guid(guid);
                this.Name = name;
                this.Description = description;
                this.Index = index;
            }
            public WidgetDetails()
            {}
        }
        public string Name { get; set; }
        public List<WidgetDetails> Widgets { get { return _widgets; } }
    }

    public class WidgetEditorModel
    {
        public string Message { get; set; }
        public string Prefix { get; set; }
        public List<WidgetSettingsEditorModel> Settings { get; set; }

        public WidgetEditorModel(string prefix, List<WidgetSettingsEditorModel> settings)
            : this(null, prefix, settings)
        { }

        public WidgetEditorModel(string message, string prefix, List<WidgetSettingsEditorModel> settings)
        {
            this.Message = message;
            this.Prefix = prefix;
            this.Settings = settings;
        }
    }

    public class WidgetSettings : IWidgetSettings
    {
        [WidgetSetting("Виден на странице", "Видимость виджета на странице", WidgetSettingControlType.CheckBox)]
        public bool Visible { get; set; }       
    }

    public class WidgetSettingsEditorModel
    {
        public string Name { get; set; }
        public object Value { get; set; }
        public WidgetSettingAttribute Annotation { get; set; }

        public WidgetSettingsEditorModel()
        { }

        public WidgetSettingsEditorModel(string name, object value, WidgetSettingAttribute annotation)
        {
            this.Name = name;
            this.Value = value;
            this.Annotation = annotation;
        }
    }  
}
